package com.example.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.project.entity.Budget;
import com.example.project.entity.Depart;
import com.example.project.entity.Project;
import com.example.project.mapper.BudgetMapper;
import com.example.project.mapper.DepartMapper;
import com.example.project.mapper.ProjectMapper;
import com.example.project.service.BudgetService;
import com.example.project.service.DepartService;
import com.example.project.vo.model.PageDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * (Depart)表服务实现类
 */
@Service("BudgetService")
public class BudgetServiceImpl extends ServiceImpl<BudgetMapper, Budget>
        implements BudgetService {

    @Autowired
    private ProjectMapper projectMapper;


    @Override
    public Budget queryById(String id) {
        return this.getBaseMapper().selectById(id);
    }

    @Override
    public PageInfo<Budget> queryAll(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Budget> list = this.getBaseMapper().selectList(pageDto.toQueryWrapper());
        return new PageInfo<>(list);
    }

    @Override
    public Budget insert(Budget budget) {
        Long projectId = budget.getProjectId();
        Project project = projectMapper.selectById(projectId);

        String projName = project.getProjName();
        budget.setProjectName(projName);
        budget.setId(UUID.randomUUID().toString());
        this.getBaseMapper().insert(budget);
        return budget;
    }

    @Override
    public Budget update(Budget budget) {
        this.getBaseMapper().updateById(budget);
        return this.queryById(budget.getId());
    }

    @Override
    public List<Budget> querryList() {
        return this.getBaseMapper().selectList(null);
    }

    @Override
    public boolean deleteById(String id) {
        return this.getBaseMapper().deleteById(id) > 0;
    }
}
